package controllers;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import dao.EmployeeDAORemote;
import dto.EmployeeDTO;
import emailbeans.EmployeeEmailBean;

@ManagedBean
@SessionScoped
public class AdminController implements Serializable{
	private static final long serialVersionUID = 1L;

	@EJB
	EmployeeDAORemote employeeDAO;

	@ManagedProperty(value = "#{employeeDTO}")
	EmployeeDTO employeeDTO;
	
	@ManagedProperty(value="#{employeeEmailBean}")
	EmployeeEmailBean employeeEmailBean;

	private List<String> checkbox = new ArrayList<String>();
	private List<EmployeeDTO> listOfAdmins;
	
	
	@PostConstruct
	public void initController(){
		listOfAdmins=employeeDAO.getAllAdmins();
	}
	
	public void addAdmin(ActionEvent event){
		try{
			if(employeeDTO.getName()=="" || employeeDTO.getName()==null){
				FacesContext context = FacesContext.getCurrentInstance(); 
				context.addMessage(event.getComponent().getClientId(),new FacesMessage("Please, type a name for the administrator!"));
			}else if(employeeDTO.getName().split(" ").length<2){
				FacesContext context = FacesContext.getCurrentInstance(); 
				context.addMessage(event.getComponent().getClientId(),new FacesMessage("Please, type a valid name for the administrator. Ex: Thomas Tailor"));
			}else if(!employeeDTO.getEmail().contains("@")){
				FacesContext context = FacesContext.getCurrentInstance(); 
				context.addMessage(event.getComponent().getClientId(),new FacesMessage("Please, type a valid email for the administrator!"));
			}else{
				employeeDTO.setRank(1);
				String t[]=employeeDTO.getName().split(" ");
				employeeDTO.setUsername(t[0]+"."+t[1]);
				employeeDAO.addEmployee(employeeDTO);

				listOfAdmins=employeeDAO.getAllAdmins();
				
				EmployeeDTO ed=employeeDAO.getEmployeeByEmail(employeeDTO.getEmail());
				employeeEmailBean.setEmployee(ed);
				
				employeeDTO.setName("");
				employeeDTO.setEmail("");
				
				FacesContext
				.getCurrentInstance()
				.getExternalContext()
				.redirect(
						"/MSGCandidateTesting/faces/newAccountEmail.xhtml");
				
			}
		}catch(Exception e){
			FacesContext context = FacesContext.getCurrentInstance(); 
			context.addMessage(event.getComponent().getClientId(),new FacesMessage("Error adding administrator!"));
		}
	}
	
	public void deleteAdmins(ActionEvent event){
		if(checkbox.size()!=0){
			for(String s:checkbox){
				EmployeeDTO ed=new EmployeeDTO();
				ed.setId(Integer.parseInt(s));
				try {
					employeeDAO.deleteEmployee(ed);
				} catch (Exception e) {
					FacesContext context = FacesContext.getCurrentInstance(); 
					context.addMessage(event.getComponent().getClientId(),new FacesMessage("Error deleting administrator!"));
					System.out.println(e.getMessage());
					e.printStackTrace();
				}
			}
			checkbox.clear();
			listOfAdmins=employeeDAO.getAllAdmins();
		}else{
			FacesContext context = FacesContext.getCurrentInstance(); 
			String clientId = event.getComponent().getClientId(context); 
			context.addMessage( clientId, new FacesMessage("Please, select at least one administrator!") );
		}
	}
	
	/////GETERS/SETTERS
	
	
	
	public EmployeeDTO getEmployeeDTO() {
		return employeeDTO;
	}

	public EmployeeEmailBean getEmployeeEmailBean() {
		return employeeEmailBean;
	}

	public void setEmployeeEmailBean(EmployeeEmailBean employeeEmailBean) {
		this.employeeEmailBean = employeeEmailBean;
	}

	public void setEmployeeDTO(EmployeeDTO employeeDTO) {
		this.employeeDTO = employeeDTO;
	}

	public List<String> getCheckbox() {
		return checkbox;
	}

	public void setCheckbox(List<String> checkbox) {
		this.checkbox.addAll(checkbox);
	}

	public List<EmployeeDTO> getListOfAdmins() {
		return listOfAdmins;
	}

	public void setListOfAdmins(List<EmployeeDTO> listOfAdmins) {
		this.listOfAdmins = listOfAdmins;
	}
	
	
	
}
